{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "# tulipy\n",
    "\n",
    "## Python bindings for [Tulip Indicators](https://tulipindicators.org/)\n",
    "\n",
    "Tulipy requires [numpy](http://www.numpy.org/) as all inputs and outputs are numpy arrays (`dtype=np.float64`).\n",
    "\n",
    "## Installation\n",
    "\n",
    "You can install via `pip install tulipy`.\n",
    "If a wheel is not available for your system, you will need to `pip install Cython numpy` to build from the source distribution.\n",
    "\n",
    "## Usage"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import tulipy as ti"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.8.4'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ti.TI_VERSION"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "DATA = np.array([81.59, 81.06, 82.87, 83,    83.61,\n",
    "                 83.15, 82.84, 83.99, 84.55, 84.36,\n",
    "                 85.53, 86.54, 86.89, 87.77, 87.29])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Information about indicators are exposed as properties:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def print_info(indicator):\n",
    "    print(\"Type:\", indicator.type)\n",
    "    print(\"Full Name:\", indicator.full_name)\n",
    "    print(\"Inputs:\", indicator.inputs)\n",
    "    print(\"Options:\", indicator.options)\n",
    "    print(\"Outputs:\", indicator.outputs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: simple\n",
      "Full Name: Vector Square Root\n",
      "Inputs: ['real']\n",
      "Options: []\n",
      "Outputs: ['sqrt']\n"
     ]
    }
   ],
   "source": [
    "print_info(ti.sqrt)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Single outputs are returned directly. Indicators returning multiple outputs use\n",
    "a tuple in the order indicated by the `outputs` property."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 9.03271831,  9.00333272,  9.10329611,  9.11043358,  9.14385039,\n",
       "        9.11866218,  9.1016482 ,  9.16460583,  9.19510739,  9.18477   ,\n",
       "        9.24824308,  9.30268778,  9.32148057,  9.36856446,  9.34291175])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ti.sqrt(DATA)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: overlay\n",
      "Full Name: Simple Moving Average\n",
      "Inputs: ['real']\n",
      "Options: ['period']\n",
      "Outputs: ['sma']\n"
     ]
    }
   ],
   "source": [
    "print_info(ti.sma)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 82.426,  82.738,  83.094,  83.318,  83.628,  83.778,  84.254,\n",
       "        84.994,  85.574,  86.218,  86.804])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ti.sma(DATA, period=5)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Invalid options will throw an `InvalidOptionError`:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Invalid Option!\n"
     ]
    }
   ],
   "source": [
    "try:\n",
    "    ti.sma(DATA, period=-5)\n",
    "except ti.InvalidOptionError:\n",
    "    print(\"Invalid Option!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Type: overlay\n",
      "Full Name: Bollinger Bands\n",
      "Inputs: ['real']\n",
      "Options: ['period', 'stddev']\n",
      "Outputs: ['bbands_lower', 'bbands_middle', 'bbands_upper']\n"
     ]
    }
   ],
   "source": [
    "print_info(ti.bbands)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ 80.53004219,  80.98714192,  82.53334324,  82.47198345,\n",
       "         82.41775044,  82.43520292,  82.51133078,  83.14261781,\n",
       "         83.53648779,  83.8703237 ,  85.28887096]),\n",
       " array([ 82.426,  82.738,  83.094,  83.318,  83.628,  83.778,  84.254,\n",
       "         84.994,  85.574,  86.218,  86.804]),\n",
       " array([ 84.32195781,  84.48885808,  83.65465676,  84.16401655,\n",
       "         84.83824956,  85.12079708,  85.99666922,  86.84538219,\n",
       "         87.61151221,  88.5656763 ,  88.31912904]))"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ti.bbands(DATA, period=5, stddev=2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "If inputs of differing sizes are provided, they are right-aligned and trimmed from the left:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "DATA2 = np.array([83.15, 82.84, 83.99, 84.55, 84.36])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  50.,  100.,   50.])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 'high' trimmed to DATA[-5:] == array([ 85.53,  86.54,  86.89,  87.77,  87.29])\n",
    "ti.aroonosc(high=DATA, low=DATA2, period=2)"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
